/* 
 * File:   Player.h
 * Author: lucia
 *
 * Created on 27 de octubre de 2014, 17:00
 */

#ifndef PLAYER_H
#define	PLAYER_H


#include <string>
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>

#include "../Controller/SBSLog.h"
#include "../Controller/BlockingQueueEvent.h"
#include "../Controller/BlockingQueueObject.h"
#include "../Define/DefaultValues.h"
#include "../Define/Structs.h"
#include "ClientSocket.h"


class Player {
private:
    //Interaccion con el servidor
    vector<std::thread> threads;
    
//    std::thread threadRecv;
//    std::thread threadSend;
//    std::thread threadRecv2;
//    std::thread threadSend2;
    
    BlockingQueueEvent events; 
    BlockingQueueObject objects;
    ClientSocket* client;
    std::mutex mtx;
    std::mutex ptx;
    bool init;
    bool semaforo;
    int pack;
    bool endThreads;
    
    // marca la espera cuando un jugador se conecta a un juego reload
    bool waitingForStart;
    
public:
    Player(ClientSocket* client);
    //mutex queue
    //void commuQueue(int i , struct ObjectInit &e);
    //Interaccion con el servidor
    struct ObjectInit popObjects();
    void pushObjects(struct ObjectInit buffer);
    void communicationRecv();
    void communicationRecv2();
    void communicationSend();
    struct Event popEvent();
    void pushEvent(struct Event event);
    void runThreadRecv();
    void runThreadSend();
    void runThreadRecv2();
    void runThreadSend2();
    //int commuClient(int i, void* e, int length);
    void send(struct Event e);
    //struct ObjectInit getEvent();
    //void setEvent(struct ObjectInit &event);
    
    bool tryPop(struct ObjectInit &obj);
    bool isInit();
    void setInit(bool flag);
    void setWaitForReloadGame(int flag);
    int getCantPack();
    void setEndThreads(bool threadstate);
    virtual ~Player();

};

#endif	/* PLAYER_H */

